<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>消息管理</span>
        </div>
      </template>

      <!-- 搜索表单 -->
      <div class="search-form">
        <el-form :model="searchForm" :inline="true">
          <el-form-item label="发送者">
            <el-input v-model="searchForm.sender" placeholder="请输入发送者" clearable />
          </el-form-item>
          <el-form-item label="消息类型">
            <el-select v-model="searchForm.type" placeholder="请选择类型" clearable>
              <el-option label="文本" value="TEXT" />
              <el-option label="图片" value="IMAGE" />
              <el-option label="文件" value="FILE" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="sender" label="发送者" width="120" />
        <el-table-column prop="receiver" label="接收者" width="120" />
        <el-table-column prop="content" label="消息内容" />
        <el-table-column prop="type" label="类型" width="100">
          <template #default="scope">
            <el-tag>{{ getTypeText(scope.row.type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发送时间" width="180">
          <template #default="scope">
            {{ formatDateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <div class="table-operation-wrapper">
              <el-button
                type="danger"
                icon="Delete"
                @click="handleDelete(scope.row)"
                class="table-operation-button is-danger"
                size="small"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.pageSize"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)

// 搜索表单
const searchForm = reactive({
  sender: '',
  type: ''
})

// 分页数据
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0
})

// 表格数据
const tableData = ref([])

// 获取数据列表
const getList = async () => {
  loading.value = true
  try {
    // 模拟API调用
    const mockData = {
      records: [
        {
          id: 1,
          sender: '张三',
          receiver: '李四',
          content: '你好，请问有什么可以帮助你的吗？',
          type: 'TEXT',
          createTime: '2024-01-15 10:30:00'
        }
      ],
      total: 1
    }
    
    tableData.value = mockData.records
    pagination.total = mockData.total
  } catch (error) {
    console.error('获取消息列表失败:', error)
    ElMessage.error('获取消息列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  getList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    sender: '',
    type: ''
  })
  handleSearch()
}

// 删除
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这条消息吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    ElMessage.success('删除成功')
    getList()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 分页处理
const handleSizeChange = (size) => {
  pagination.pageSize = size
  getList()
}

const handleCurrentChange = (current) => {
  pagination.current = current
  getList()
}

// 工具函数
const getTypeText = (type) => {
  const typeMap = {
    'TEXT': '文本',
    'IMAGE': '图片',
    'FILE': '文件'
  }
  return typeMap[type] || '未知'
}

const formatDateTime = (datetime) => {
  if (!datetime) return '-'
  return datetime
}

// 页面加载
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>
